<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class usergroup_api{

	public function getSystemUserGroup(){
		return load_db('user/usergroup')->getGroupCache('system');
	}

	public function getSpecialUserGroup(){
		return load_db('user/usergroup')->getGroupCache('special');
	}

	public function getMemberUserGroup(){
		return load_db('user/usergroup')->getGroupCache('member');
	}

	public function getGroupByid($gid,$type){
		$cache = load_db('user/usergroup')->getGroupCache($type);
		return $cache[$gid];
	}

	public function getGroupByKey($key,$type){
		$cache = load_db('user/usergroup')->getGroupCache($type);
		foreach ($cache as $g){
			if($g['usergroup_key']==$key){
				return $g;
			}
		}
		return false;
	}
	
	public function deleteGroupByid($gid){
		$usergroupdb = load_db('user/usergroup');
		if($g = $this->getGroupByid($gid)){
			$usergroupdb->deleteBy('usergroup_id',$gid);
			return $usergroupdb->updateGroupCache($g['usergroup_type']);
		}
		return false;
	}

	public function addGroup($data){
		$usergroupdb = load_db('user/usergroup');
		if($usergroupdb->add_one($data)){
			return $usergroupdb->updateGroupCache($data['usergroup_type']);
		}
		return false;
	}
	

	public function updateGroup($gid,$data){
		$usergroupdb = load_db('user/usergroup');
		if($usergroupdb->update(array('usergroup_id'=>$gid),$data)){
			return $usergroupdb->updateGroupCache($data['usergroup_type']);
		}
		return false;
	}
	
}
